//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Provider.Service;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Iterator;
import java.util.List;
import sun.security.jca.GetInstance;
import sun.security.jca.GetInstance.Instance;
import sun.security.util.Debug;

public class KeyGenerator {
    private static final Debug pdebug = Debug.getInstance("provider", "Provider");
    private static final boolean skipDebug = Debug.isOn("engine=") && !Debug.isOn("keygenerator");
    private static final int I_NONE = 1;
    private static final int I_RANDOM = 2;
    private static final int I_PARAMS = 3;
    private static final int I_SIZE = 4;
    private Provider provider;
    private volatile javax.vpncrypto.KeyGeneratorSpi spi;
    private final String algorithm;
    private final Object lock = new Object();
    private Iterator<Service> serviceIterator;
    private int initType;
    private int initKeySize;
    private AlgorithmParameterSpec initParams;
    private SecureRandom initRandom;

    protected KeyGenerator(javax.vpncrypto.KeyGeneratorSpi var1, Provider var2, String var3) {
        this.spi = var1;
        this.provider = var2;
        this.algorithm = var3;
        if (!skipDebug && pdebug != null) {
            pdebug.println("KeyGenerator." + var3 + " algorithm from: " + this.provider.getName());
        }

    }

    private KeyGenerator(String var1) throws NoSuchAlgorithmException {
        this.algorithm = var1;
        List var2 = GetInstance.getServices("KeyGenerator", var1);
        this.serviceIterator = var2.iterator();
        this.initType = 1;
        if (this.nextSpi((javax.vpncrypto.KeyGeneratorSpi)null, false) == null) {
            throw new NoSuchAlgorithmException(var1 + " KeyGenerator not available");
        } else {
            if (!skipDebug && pdebug != null) {
                pdebug.println("KeyGenerator." + var1 + " algorithm from: " + this.provider.getName());
            }

        }
    }

    public final String getAlgorithm() {
        return this.algorithm;
    }

    public static final KeyGenerator getInstance(String var0) throws NoSuchAlgorithmException {
        return new KeyGenerator(var0);
    }

    public static final KeyGenerator getInstance(String var0, String var1) throws NoSuchAlgorithmException, NoSuchProviderException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("KeyGenerator", javax.vpncrypto.KeyGeneratorSpi.class, var0, var1);
        return new KeyGenerator((javax.vpncrypto.KeyGeneratorSpi)var2.impl, var2.provider, var0);
    }

    public static final KeyGenerator getInstance(String var0, Provider var1) throws NoSuchAlgorithmException {
        Instance var2 = javax.vpncrypto.JceSecurity.getInstance("KeyGenerator", javax.vpncrypto.KeyGeneratorSpi.class, var0, var1);
        return new KeyGenerator((javax.vpncrypto.KeyGeneratorSpi)var2.impl, var2.provider, var0);
    }

    public final Provider getProvider() {
        synchronized(this.lock) {
            this.disableFailover();
            return this.provider;
        }
    }

    private javax.vpncrypto.KeyGeneratorSpi nextSpi(javax.vpncrypto.KeyGeneratorSpi var1, boolean var2) {
        synchronized(this.lock) {
            if (var1 != null && var1 != this.spi) {
                return this.spi;
            } else if (this.serviceIterator == null) {
                return null;
            } else {
                while(true) {
                    Service var4;
                    do {
                        if (!this.serviceIterator.hasNext()) {
                            this.disableFailover();
                            return null;
                        }

                        var4 = (Service)this.serviceIterator.next();
                    } while(!javax.vpncrypto.JceSecurity.canUseProvider(var4.getProvider()));

                    try {
                        Object var5 = var4.newInstance((Object)null);
                        if (var5 instanceof javax.vpncrypto.KeyGeneratorSpi) {
                            javax.vpncrypto.KeyGeneratorSpi var6 = (javax.vpncrypto.KeyGeneratorSpi)var5;
                            if (var2) {
                                if (this.initType == 4) {
                                    var6.engineInit(this.initKeySize, this.initRandom);
                                } else if (this.initType == 3) {
                                    var6.engineInit(this.initParams, this.initRandom);
                                } else if (this.initType == 2) {
                                    var6.engineInit(this.initRandom);
                                } else if (this.initType != 1) {
                                    throw new AssertionError("KeyGenerator initType: " + this.initType);
                                }
                            }

                            this.provider = var4.getProvider();
                            this.spi = var6;
                            javax.vpncrypto.KeyGeneratorSpi var10000 = var6;
                            return var10000;
                        }
                    } catch (Exception var8) {
                    }
                }
            }
        }
    }

    void disableFailover() {
        this.serviceIterator = null;
        this.initType = 0;
        this.initParams = null;
        this.initRandom = null;
    }

    public final void init(SecureRandom var1) {
        if (this.serviceIterator == null) {
            this.spi.engineInit(var1);
        } else {
            RuntimeException var2 = null;
            javax.vpncrypto.KeyGeneratorSpi var3 = this.spi;

            while(true) {
                try {
                    var3.engineInit(var1);
                    this.initType = 2;
                    this.initKeySize = 0;
                    this.initParams = null;
                    this.initRandom = var1;
                    return;
                } catch (RuntimeException var5) {
                    if (var2 == null) {
                        var2 = var5;
                    }

                    var3 = this.nextSpi(var3, false);
                    if (var3 == null) {
                        throw var2;
                    }
                }
            }
        }
    }

    public final void init(AlgorithmParameterSpec var1) throws InvalidAlgorithmParameterException {
        this.init(var1, javax.vpncrypto.JceSecurity.RANDOM);
    }

    public final void init(AlgorithmParameterSpec var1, SecureRandom var2) throws InvalidAlgorithmParameterException {
        if (this.serviceIterator == null) {
            this.spi.engineInit(var1, var2);
        } else {
            Exception var3 = null;
            javax.vpncrypto.KeyGeneratorSpi var4 = this.spi;

            while(true) {
                try {
                    var4.engineInit(var1, var2);
                    this.initType = 3;
                    this.initKeySize = 0;
                    this.initParams = var1;
                    this.initRandom = var2;
                    return;
                } catch (Exception var6) {
                    if (var3 == null) {
                        var3 = var6;
                    }

                    var4 = this.nextSpi(var4, false);
                    if (var4 == null) {
                        if (var3 instanceof InvalidAlgorithmParameterException) {
                            throw (InvalidAlgorithmParameterException)var3;
                        }

                        if (var3 instanceof RuntimeException) {
                            throw (RuntimeException)var3;
                        }

                        throw new InvalidAlgorithmParameterException("init() failed", var3);
                    }
                }
            }
        }
    }

    public final void init(int var1) {
        this.init(var1, javax.vpncrypto.JceSecurity.RANDOM);
    }

    public final void init(int var1, SecureRandom var2) {
        if (this.serviceIterator == null) {
            this.spi.engineInit(var1, var2);
        } else {
            RuntimeException var3 = null;
            javax.vpncrypto.KeyGeneratorSpi var4 = this.spi;

            while(true) {
                try {
                    var4.engineInit(var1, var2);
                    this.initType = 4;
                    this.initKeySize = var1;
                    this.initParams = null;
                    this.initRandom = var2;
                    return;
                } catch (RuntimeException var6) {
                    if (var3 == null) {
                        var3 = var6;
                    }

                    var4 = this.nextSpi(var4, false);
                    if (var4 == null) {
                        throw var3;
                    }
                }
            }
        }
    }

    public final SecretKey generateKey() {
        if (this.serviceIterator == null) {
            return this.spi.engineGenerateKey();
        } else {
            RuntimeException var1 = null;
            KeyGeneratorSpi var2 = this.spi;

            while(true) {
                try {
                    return var2.engineGenerateKey();
                } catch (RuntimeException var4) {
                    if (var1 == null) {
                        var1 = var4;
                    }

                    var2 = this.nextSpi(var2, true);
                    if (var2 == null) {
                        throw var1;
                    }
                }
            }
        }
    }
}
